package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.*;
import org.jeecg.glr.service.*;
import org.jeecg.glr.util.GLRConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 路灯灯杆管理
 * @Author: jeecg-boot
 * @Date: 2020-03-05
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrLightPoleDevice")
@Slf4j
public class GlrLightPoleDeviceController extends JeecgController<GlrLightPoleDevice, IGlrLightPoleDeviceService> {
    @Autowired
    private IGlrLightPoleDeviceService glrLightPoleDeviceService;
    @Autowired
    private IGlrRadioService glrRadioService;
    @Autowired
    private IGlrLedService glrLedService;
    @Autowired
    private IGlrLightService glrLightService;
    @Autowired
    private IGlrGatewayService glrGatewayService;
    @Autowired
    private IGlrVideoService glrVideoService;
    @Autowired
    private IGlrSensorService glrSensorService;
    @Autowired
    private IGlrVideoYsService glrVideoYsService;
    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(GlrLightPoleDevice glrLightPoleDevice,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<GlrLightPoleDevice> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLightPoleDevice, req.getParameterMap());
        Page<GlrLightPoleDevice> page = new Page<GlrLightPoleDevice>(pageNo, pageSize);
        IPage<GlrLightPoleDevice> pageList = glrLightPoleDeviceService.page(page, queryWrapper);
       List<GlrLightPoleDevice> list= pageList.getRecords();
        for (GlrLightPoleDevice glr :
                list) {
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_01)){
                GlrRadio radio=glrRadioService.getById(glr.getDeviceId());
                if (radio!=null){
                    glr.setDeviceName(radio.getName());
                }
            }
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_02)){
                GlrLed led=glrLedService.getById(glr.getDeviceId());
                if (led!=null){
                    glr.setDeviceName(led.getName());
                }
            }
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_03)){
                GlrLight lg=glrLightService.getById(glr.getDeviceId());
                if (lg!=null){
                    glr.setDeviceName(lg.getCode());
                }
            }
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_04)){
                GlrGateway gateway=glrGatewayService.getById(glr.getDeviceId());
                if (gateway!=null){
                    glr.setDeviceName(gateway.getGatewayName());
                }
            }
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_05)){
                GlrSensor sensor=glrSensorService.getById(glr.getDeviceId());
                if (sensor!=null){
                    glr.setDeviceName(sensor.getName());
                }
            }
            if(glr.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_06)){
                GlrVideo video=glrVideoService.getById(glr.getDeviceId());
                if (video!=null){
                    glr.setDeviceName(video.getName());
                }
            }
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody GlrLightPoleDevice glrLightPoleDevice) {
        glrLightPoleDeviceService.save(glrLightPoleDevice);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody GlrLightPoleDevice glrLightPoleDevice) {
        glrLightPoleDeviceService.updateById(glrLightPoleDevice);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        glrLightPoleDeviceService.removeEntityById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.glrLightPoleDeviceService.removeEntityByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        GlrLightPoleDevice glrLightPoleDevice = glrLightPoleDeviceService.getById(id);
        if (glrLightPoleDevice == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(glrLightPoleDevice);
    }

    /**
     * 导出excel
     *
     * @param request
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrLightPoleDevice glrLightPoleDevice) {
        return super.exportXls(request, glrLightPoleDevice, GlrLightPoleDevice.class, "路灯管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrLightPoleDevice.class);
    }


    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/lightPoleDeviceList")
    public Result<?> lightPoleDeviceList(GlrLightPoleDevice glrLightPoleDevice,
                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                             HttpServletRequest req) {
        QueryWrapper<GlrLightPoleDevice> queryWrapper = QueryGenerator.initQueryWrapperCode(glrLightPoleDevice, req.getParameterMap());
        Page<GlrLightPoleDevice> page = new Page<GlrLightPoleDevice>(pageNo, pageSize);
        IPage<GlrLightPoleDevice> pageList = glrLightPoleDeviceService.page(page, queryWrapper);
        List<GlrLightPoleDevice> list = pageList.getRecords();
 /*       for (GlrLightPoleDevice m : list) {
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_101){
                GlrRadio glrRadio=glrRadioService.getById(m.getDeviceId());
                if (glrRadio!=null) m.setDeviceName(glrRadio.getName());
            }
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_102){
                GlrLed glrLed=glrLedService.getById(m.getDeviceId());
                if (glrLed!=null) m.setDeviceName(glrLed.getName());
            }
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_103){
                GlrLight glrLight=glrLightService.getById(m.getDeviceId());
                if (glrLight!=null) m.setDeviceName("("+glrLight.getGateWayCode()+")"+glrLight.getCode());
            }
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_104){
                GlrGateway glrGateway=glrGatewayService.getById(m.getDeviceId());
                if (glrGateway!=null) m.setDeviceName("("+glrGateway.getGateWayCode()+")"+glrGateway.getGatewayName());
            }
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_105){
                GlrSensor glrSensor=glrSensorService.getById(m.getDeviceId());
                if (glrSensor!=null) m.setDeviceName(glrSensor.getName());
            }
            if (m.getDeviceType()== GLRConstant.GLR_LIGHT_POLE_DEVICE_106){
                GlrVideo glrVideor=glrVideoService.getById(m.getDeviceId());
                if (glrVideor!=null) m.setDeviceName(glrVideor.getName());
            }
        }*/
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/lightPoleDeviceListAdd")
    public Result<?> lightPoleDeviceListAdd(
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String lightPoleCode=req.getParameter("paramLightPoleCode");
        int deviceType=Integer.parseInt(req.getParameter("paramDeviceType"));
        String stSql="select device_id from glr_light_pole_device where light_pole_code='"+lightPoleCode+"' and device_type="+deviceType;
        List<Map<String, Object>> mapList = glrVideoYsService.doGetListForJdbc(stSql);
        List<String> strList=new ArrayList<>();
        for(Map<String, Object> o:mapList){
            strList.add(o.get("device_id").toString());
        }
        Map<String, String[]> hashMap=new HashMap<>();

        /*if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_101){
            QueryWrapper<GlrRadio> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrRadio(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrRadio> page = new Page<GlrRadio>(pageNo, pageSize);
            IPage<GlrRadio> pageList = glrRadioService.page(page, queryWrapper);
            return Result.ok(pageList);
        }
        if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_102){
            QueryWrapper<GlrLed> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrLed(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrLed> page = new Page<GlrLed>(pageNo, pageSize);
            IPage<GlrLed> pageList = glrLedService.page(page, queryWrapper);
            return Result.ok(pageList);
        }
        if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_103){
            QueryWrapper<GlrLight> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrLight(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrLight> page = new Page<GlrLight>(pageNo, pageSize);
            IPage<GlrLight> pageList = glrLightService.page(page, queryWrapper);
            List<GlrLight> list=pageList.getRecords();
            List<JSONObject> jsonObjectList=new ArrayList<>();
            for (GlrLight lg :list) {
                try {
                    JSONObject json = JSONObject.fromObject(lg);
                    json.put("name","("+json.get("gateWayCode").toString()+")"+json.get("code").toString());
                    jsonObjectList.add(json);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Page<JSONObject> pageList1 = new Page<JSONObject>(pageNo, pageSize);
            pageList1.setRecords(jsonObjectList);
            pageList1.setTotal(pageList.getTotal());
            pageList1.setSize(pageList.getSize());
            pageList1.setCurrent(pageList.getCurrent());
            return Result.ok(pageList1);
        }
        if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_104){
            QueryWrapper<GlrGateway> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrGateway(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrGateway> page = new Page<GlrGateway>(pageNo, pageSize);
            IPage<GlrGateway> pageList = glrGatewayService.page(page, queryWrapper);
            List<GlrGateway> list=pageList.getRecords();
            List<JSONObject> jsonObjectList=new ArrayList<>();
            for (GlrGateway lg :list) {
                try {
                    JSONObject json = JSONObject.fromObject(lg);
                    json.put("name","("+json.get("gateWayCode").toString()+")"+json.get("gatewayName").toString());
                    jsonObjectList.add(json);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Page<JSONObject> pageList1 = new Page<JSONObject>(pageNo, pageSize);
            pageList1.setRecords(jsonObjectList);
            pageList1.setTotal(pageList.getTotal());
            pageList1.setSize(pageList.getSize());
            pageList1.setCurrent(pageList.getCurrent());
            return Result.ok(pageList1);
        }
        if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_105){
            QueryWrapper<GlrSensor> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrSensor(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrSensor> page = new Page<GlrSensor>(pageNo, pageSize);
            IPage<GlrSensor> pageList = glrSensorService.page(page, queryWrapper);
            return Result.ok(pageList);
        }
        if (deviceType== GLRConstant.GLR_LIGHT_POLE_DEVICE_106){
            QueryWrapper<GlrVideo> queryWrapper = QueryGenerator.initQueryWrapperCode(new GlrVideo(), hashMap);
            if(strList!=null&&strList.size()>0){
                queryWrapper.notIn("id", strList);
            }
            Page<GlrVideo> page = new Page<GlrVideo>(pageNo, pageSize);
            IPage<GlrVideo> pageList = glrVideoService.page(page, queryWrapper);
            return Result.ok(pageList);
        }
*/
        return Result.error("没有找到");

    }

    /**
     * 通过id查询
     *
     * @return
     */
    @PostMapping(value = "/bingPoleDevice")
    public Result<?> bingPoleDevice(@RequestBody GlrLightPoleDevice glrLightPoleDevice) {
        glrLightPoleDeviceService.bingLightPoleDevice(glrLightPoleDevice);
        return Result.ok();
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @GetMapping(value = "/glrLightPoleDeviceList")
    public Result<?> glrLightPoleDeviceList(GlrLightPoleDevice glrLightPoleDevice) {
       List<GlrLightPoleDevice> list= glrLightPoleDeviceService.glrLightPoleDeviceListById(glrLightPoleDevice.getLightPoleCode(),glrLightPoleDevice.getProjectCode());
        for (GlrLightPoleDevice glr :
                list) {
            
        }
        return Result.ok(list);
    }





}
